package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.*;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.dc.pr.PRError;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private SpuService spuService;

    @Autowired
    private SkuService skuService;

    @Autowired
    private SpuDetailService spuDetailService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 分页查询spu表
     *
     * @param page
     * @param rows
     * @param brandId
     * @param categoryId
     * @param id
     * @param saleable
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuPage(
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "rows", defaultValue = "5") Long rows,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "saleable", required = false) Boolean saleable
    ) {
        return ResponseEntity.ok(spuService.querySpuPage(page, rows, brandId, categoryId, id, saleable));
    }


    /**
     * 根据spuId,查询spu的所有规格参数,值value
     *
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpuValuesBySpuId(
            @RequestParam("id") Long spuId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(specParamService.querySpuValuesBySpuId(spuId, searching));
    }


    /**
     * 根据id批量查询sku
     *
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(skuService.querySkuListByIds(ids));
    }


    /**
     * 根据商品spuID查询sku的集合
     *
     * @param spuID
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuID(@RequestParam("id") Long spuID) {
        return ResponseEntity.ok(skuService.querySkuListBySpuID(spuID));
    }


    /**
     * 根据商品spuID查询商品详情
     *
     * @param spuID
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuID(@RequestParam("id") Long spuID) {
        return ResponseEntity.ok(spuDetailService.querySpuDetailBySpuID(spuID));
    }


    /**
     * 根据商品spuID查询spu及sku、spuDetail等
     *
     * @param spuID
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsBySpuID(@PathVariable("id") Long spuID) {
        return ResponseEntity.ok(spuService.queryGoodsBySpuID(spuID));
    }


    /**
     * 根据商品spuID查询商品
     *
     * @param spuID
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuBySpuID(@PathVariable("id") Long spuID) {
        return ResponseEntity.ok(spuService.querySpuBySpuID(spuID));
    }


    /**
     * 修改商品上下架
     * sku,spu都需要下架，可以发现唯独这两表有saleable字段
     *
     * @param supId
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSaleable(
            @RequestParam(value = "id", required = false) Long supId,
            @RequestParam(value = "saleable", required = false) Boolean saleable
    ) {
        spuService.updateSaleable(supId, saleable);
        return ResponseEntity.ok().build();
    }


    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO ) {
        spuService.addGoods(spuDTO);
        return ResponseEntity.ok().build();
    }


    /**
     * 更新商品
     * @param spuDTO
     * @return
     */
    @PutMapping("spu")
    public ResponseEntity<Void> UpdateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.UpdateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }


    /**
     * 商品减库存
     * @param cartsMap
     * @return
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> subtractStock(@RequestBody Map<Long, Integer> cartsMap) {
        skuService.subtractStock(cartsMap);
        return ResponseEntity.ok().build();
    }

}

